package com.cwj.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

class JFileChooserDemo
{

    private JLabel label=new JLabel("所选文件路径：");
    private JTextField jtf=new JTextField(25);
    private JButton button=new JButton("浏览");
    public JFileChooserDemo()
    {
        JFrame jf=new JFrame("文件选择器");
        JPanel panel=new JPanel();
        panel.add(label);
        panel.add(jtf);
        panel.add(button);
        jf.add(panel);
        jf.pack();    //自动调整大小
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        button.addActionListener(new MyActionListener());    //监听按钮事件
    }
    //Action事件处理
    class MyActionListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent arg0)
        {
            JFileChooser fc=new JFileChooser(".\\");
            int val=fc.showOpenDialog(null);    //文件打开对话框
            if(val==fc.APPROVE_OPTION)
            {
                //正常选择文件
                jtf.setText(fc.getSelectedFile().toString());
            }
            else
            {
                //未正常选择文件，如选择取消按钮
                jtf.setText("未选择文件");
            }
        }
    }
    public static void main(String[] args)
    {
       new myDialog();
        return;
    }
}
class myDialog extends JDialog{
    JButton b1=new JButton();
    JButton b2=new JButton();

    public myDialog(){
        JPanel panel=new JPanel();
        this.setSize(300,300);
        b1.setText("确定");
        b2.setText("取消");
        JTextField text=new JTextField();text.setColumns(11);
        panel.add(text);
        panel.add(b1);
        panel.add(b2);
        this.setContentPane(panel);
        this.setTitle("myDialog");
        panel.setSize(300,400);
        this.setVisible(true);
        b1.addActionListener((e)->{
            this.setVisible(false);
            this.dispose();
        });
        System.out.println("里面");
    }
}